home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.4 / mesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-01  |  4.9 KB  |  199 lines

  1. #include "mesh.h"
  2.  
  3. const DWORD ObjectVertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
  4.  
  5. MESH::MESH()
  6. {
  7.     m_pDevice = NULL;
  8.     m_pMesh = NULL;
  9. }
  10.  
  11. MESH::MESH(char fName[], IDirect3DDevice9* Dev)
  12. {
  13.     m_pDevice = Dev;
  14.     m_pMesh = NULL;
  15.     Load(fName, m_pDevice);
  16. }
  17.  
  18. MESH::~MESH()
  19. {
  20.     Release();
  21. }
  22.  
  23. HRESULT MESH::Load(char fName[], IDirect3DDevice9* Dev)
  24. {
  25.     m_pDevice = Dev;
  26.  
  27.     //Set white material
  28.     white.Ambient = white.Specular = white.Diffuse  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  29.     white.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
  30.     white.Power = 1.0f;
  31.  
  32.     Release();
  33.  
  34.     //Load new mesh
  35.     ID3DXBuffer * adjacencyBfr = NULL;
  36.     ID3DXBuffer * materialBfr = NULL;
  37.     DWORD noMaterials = NULL;
  38.  
  39.     if(FAILED(D3DXLoadMeshFromX(fName, D3DXMESH_MANAGED, m_pDevice,    &adjacencyBfr, &materialBfr, NULL, &noMaterials, &m_pMesh)))
  40.         return E_FAIL;
  41.  
  42.     D3DXMATERIAL *mtrls = (D3DXMATERIAL*)materialBfr->GetBufferPointer();
  43.  
  44.     for(int i=0;i<noMaterials;i++)
  45.     {
  46.         //mtrls[i].MatD3D.Ambient = mt[i].MatD3D.Diffuse;
  47.         materials.push_back(mtrls[i].MatD3D);
  48.  
  49.         if(mtrls[i].pTextureFilename != NULL)
  50.         {
  51.             char textureFileName[90];
  52.             strcpy(textureFileName, "units/");
  53.             strcat(textureFileName, mtrls[i].pTextureFilename);
  54.             IDirect3DTexture9 * newTexture = NULL;
  55.             D3DXCreateTextureFromFile(m_pDevice, textureFileName, &newTexture);            
  56.             textures.push_back(newTexture);
  57.         }
  58.         else textures.push_back(NULL);
  59.     }
  60.  
  61.     m_pMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
  62.                             (DWORD*)adjacencyBfr->GetBufferPointer(), NULL, NULL, NULL);
  63.  
  64.     adjacencyBfr->Release();
  65.     materialBfr->Release();
  66.  
  67.     return S_OK;
  68. }
  69.  
  70. void MESH::Render()
  71. {
  72.     for(int i=0;i<materials.size();i++)
  73.     {    
  74.         if(textures[i] != NULL)m_pDevice->SetMaterial(&white);
  75.         else m_pDevice->SetMaterial(&materials[i]);
  76.         m_pDevice->SetTexture(0,textures[i]);
  77.         m_pMesh->DrawSubset(i);
  78.     }    
  79. }
  80.  
  81. void MESH::Release()
  82. {
  83.     //Clear old mesh...
  84.     if(m_pMesh != NULL)
  85.     {
  86.         m_pMesh->Release();
  87.         m_pMesh = NULL;
  88.     }
  89.  
  90.     //Clear textures and materials
  91.     for(int i=0;i<textures.size();i++)
  92.         if(textures[i] != NULL)
  93.             textures[i]->Release();
  94.  
  95.     textures.clear();
  96.     materials.clear();    
  97. }
  98.  
  99. MESHINSTANCE::MESHINSTANCE()
  100. {
  101.     m_pMesh = NULL;
  102.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  103.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  104. }
  105.  
  106. MESHINSTANCE::MESHINSTANCE(MESH *meshPtr)
  107. {
  108.     m_pMesh = meshPtr;
  109.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  110.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  111. }
  112.  
  113. D3DXMATRIX MESHINSTANCE::GetWorldMatrix()
  114. {
  115.     D3DXMATRIX p, r, s;
  116.     D3DXMatrixTranslation(&p, m_pos.x, m_pos.y, m_pos.z);
  117.     D3DXMatrixRotationYawPitchRoll(&r, m_rot.y, m_rot.x, m_rot.z);
  118.     D3DXMatrixScaling(&s, m_sca.x, m_sca.y, m_sca.z);
  119.  
  120.     D3DXMATRIX world = s * r * p;
  121.     return world;
  122. }
  123.  
  124. void MESHINSTANCE::Render()
  125. {
  126.     if(m_pMesh != NULL)
  127.     {
  128.         m_pMesh->m_pDevice->SetTransform(D3DTS_WORLD, &GetWorldMatrix());
  129.         m_pMesh->Render();
  130.     }
  131. }
  132.  
  133. BBOX MESHINSTANCE::GetBoundingBox()
  134. {
  135.     if(m_pMesh == NULL || m_pMesh->m_pMesh == NULL)return BBOX();
  136.     
  137.     if(m_pMesh->m_pMesh->GetFVF() != ObjectVertex::FVF)        // XYZ and NORMAL and UV
  138.         return BBOX();
  139.  
  140.     BBOX bBox(D3DXVECTOR3(-10000.0f, -10000.0f, -10000.0f),
  141.               D3DXVECTOR3(10000.0f, 10000.0f, 10000.0f));
  142.     D3DXMATRIX World = GetWorldMatrix();
  143.  
  144.     //Lock vertex buffer of the object
  145.     ObjectVertex* vertexBuffer = NULL;
  146.     m_pMesh->m_pMesh->LockVertexBuffer(0,(void**)&vertexBuffer);
  147.  
  148.     //For each vertex in the mesh
  149.     for(int i=0;i<m_pMesh->m_pMesh->GetNumVertices();i++)
  150.     {
  151.         //Transform vertex to world space using the MESHINSTANCE
  152.         //world matrix, i.e. the position, rotation and scale
  153.         D3DXVECTOR3 pos;
  154.         D3DXVec3TransformCoord(&pos, &vertexBuffer[i]._pos, &World);
  155.  
  156.         // Check if the vertex is outside the bounds
  157.         // if so, then update the bounding volume
  158.         if(pos.x < bBox.min.x)bBox.min.x = pos.x;
  159.         if(pos.x > bBox.max.x)bBox.max.x = pos.x;
  160.         if(pos.y < bBox.min.y)bBox.min.y = pos.y;
  161.         if(pos.y > bBox.max.y)bBox.max.y = pos.y;
  162.         if(pos.z < bBox.min.z)bBox.min.z = pos.z;
  163.         if(pos.z > bBox.max.z)bBox.max.z = pos.z;
  164.     }
  165.  
  166.     m_pMesh->m_pMesh->UnlockVertexBuffer();
  167.  
  168.     return bBox;
  169. }
  170.  
  171. BSPHERE MESHINSTANCE::GetBoundingSphere()
  172. {
  173.     if(m_pMesh == NULL || m_pMesh->m_pMesh == NULL)return BSPHERE();
  174.     if(m_pMesh->m_pMesh->GetFVF() != ObjectVertex::FVF)        // XYZ and NORMAL and UV
  175.         return BSPHERE();
  176.  
  177.     BBOX bBox = GetBoundingBox();
  178.     BSPHERE bSphere;
  179.     D3DXMATRIX World = GetWorldMatrix();
  180.     bSphere.center = (bBox.max + bBox.min) / 2.0f;
  181.  
  182.     ObjectVertex* vertexBuffer = NULL;
  183.     m_pMesh->m_pMesh->LockVertexBuffer(0,(void**)&vertexBuffer);
  184.  
  185.     //Get radius
  186.     for(int i=0;i<m_pMesh->m_pMesh->GetNumVertices();i++)
  187.     {
  188.         D3DXVECTOR3 pos;
  189.         D3DXVec3TransformCoord(&pos, &vertexBuffer[i]._pos, &World);
  190.  
  191.         float l = D3DXVec3Length(&(pos - bSphere.center));
  192.         if(l > bSphere.radius)
  193.             bSphere.radius = l;
  194.     }
  195.  
  196.     m_pMesh->m_pMesh->UnlockVertexBuffer();
  197.  
  198.     return bSphere;
  199. }